package top.syfy.demo.swagger.config;

import com.google.common.base.Predicate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import springfox.documentation.RequestHandler;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.VendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;

/**
 * @author syfy
 * @date 2021/04/30
 */
@Configuration
public class SwaggerConfig {

    /**
     * 由配置文件读取的用于控制Swagger的开关
     */

    /*
    // 方式一
    @Value("${swagger.enable}")
    private boolean swaggerSwitch;
    */

    /**
     * 注入Swagger的实例
     */
    // 方式二
    @Bean
    public Docket docket(Environment env) {
        // 设置要显示的Swagger环境(如dev, test)
        Profiles profiles = Profiles.of("dev", "test");
        // 判断当前是否是处在设定的环境中(如dev, test)
        boolean swaggerFlag = env.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerFlag)
                .groupName("syfy")
                .apiInfo(apiInfo())

                .select()
                .apis(RequestHandlerSelectors.basePackage("top.syfy.demo.swagger.controller"))
                // .paths(PathSelectors.ant("/demo/swagger"))
                .build();
    }
    /*
    // 方式一
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerSwitch)
                .groupName("syfy")
                .apiInfo(apiInfo())

                .select()
                .apis(RequestHandlerSelectors.basePackage("top.syfy.demo.swagger.controller"))
                // .paths(PathSelectors.ant("/demo/swagger"))
                .build();
    }
    */

    /**
     * 注入ApiInfo的实例, 用于描述Api文档的信息
     */
    @Bean
    ApiInfo apiInfo() {
        return new ApiInfo(
                /* Api文档的标题 */
               "Api文档的标题",
                /* Api文档的描述 */
               "Api文档的描述",
                /* Api文档的版本 */
               "v1.0",
                /* 服务条款网址 */
               "http://localhost:8080/service.html",
                /* Api文档开发者的信息 */
                new Contact("syfy", "https://www.syfy.top", "aa22uu@163.com"),
                /* 许可证 */
               "Apache 2.0",
                /* 许可证URL地址 */
               "https://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList<>()
        );
    }
}
